/**
 * @Description Ext.ux.GridFormFilter is a form panel with generated fields for data filtering
 * @author  Tomislav Milkovic
 * @license LGPLv3 http://www.opensource.org/licenses/lgpl-3.0.html
 * @version 1.0, 07.09.2009.
 */

Ext.ns('Ext.ux');

Ext.ux.GridFormFilter = function(config) {
		
	Ext.apply(this, {
 	    url : '',
	 	defaults    : { width: 270 },
        region      : 'center',
        layout 		: 'form',
        border      : false,
        labelWidth  : 120,
        autoHeight  : true,
        autoWidth 	: true,		        
		bodyStyle:'padding:5px',
		mode : 1 //simple
	}); 
	
	Ext.apply(this, config || {} );
	Ext.ux.GridFormFilter.superclass.constructor.call(this);
}; // eo constructor


//config object must contain scope pointing to the grid object
//Ext.ux.GridFormFilter = Ext.extend(Ext.form.FormPanel, {

Ext.extend(Ext.ux.GridFormFilter , Ext.form.FormPanel, {	
	
	 logic : [[0, ''],[1, 'and'], [2, 'or']],
	 selection : [[1, 'contains'], [2, 'does not contain'],[3, 'like'], [4, 'not like'],[5, 'equal'], [6, 'not equal'],[7, 'greater than'],[8, 'smaller than']],
	 columns : [],
	 fset :   { xtype : 'fieldset',   
				anchor : '100%',
			    layout: 'table',
				 layoutConfig: {
			        columns: 4
			    },
			    border: false
			},
	 
	 initComponent:function() {

			Ext.apply(this, {
				 layout : this.mode==1 ? 'form' : 'table',
				 layoutConfig: {
				        columns: 4
				    },						 
				 items : this.mode==1 ? this.createFields() : this.createQFields()
			}); 
			
		   	 this.buttons = [{text   : 'Submit',
		         width       : 90,
		         handler     : function(){
		 							this.getForm().submit({
		 				                  url: this.url,
		 				                  method: 'POST',
		 				                  params: this.baseParams
		 				                  
		 				                });
		 							},
		         scale       : 'small',
		         scope       : this
			 }];			
			
			Ext.ux.GridFormFilter.superclass.initComponent.apply(this, arguments);
	},
	
	
	getNewCombo : function(ds,cfg){

		var combo = {
		    typeAhead: true,
		    editable :false,
		    triggerAction: 'all',
		    lazyRender:true,
		    mode: 'local',
		    store: new Ext.data.ArrayStore({
		        id: 0,
		        fields: ['Id', 'displayText'],
		        data: ds
		    }),
		    valueField: 'Id',
		    displayField: 'displayText'
		};
		
		combo = Ext.apply(combo,cfg);
		return new Ext.form.ComboBox(combo);
	},
	
	getRow : function(){
		var f = {items:[]};
		f.items.push(this.getNewCombo(this.columns,{width:150}));
		f.items.push(this.getNewCombo(this.selection));
		f.items.push(new Ext.form.Field({allowblank:false,anchor:'95%', width : 300}));
		var cb = this.getNewCombo(this.logic,{width:50});
		cb.on('select', this.arFormRow, this);
		f.items.push(cb);
		//return Ext.apply(f, this.fset);	
		return f.items;
	},
	
	createQFields : function(){
		
		var cm = this.scope.getColumnModel();
		
		for(i=0;i<cm.getColumnCount();i++){
			if (cm.isHidden(i)) continue;
			if (cm.getDataIndex(i)=='') continue;
			this.columns.push([cm.getDataIndex(i),cm.getColumnHeader(i)]);
		}		
		
		return this.getRow();		
	},
	
	//add remove form row event 
	arFormRow : function(combo,data,index){
		
		if (index==0){
			//delete prevoius line
			
		}else{
			//add new line
			this.add( this.getRow());
			this.doLayout();
			//alert(combo.lastSelectionText);
		}
		
	},
	
	createFields : function(){
		var fields = [];
		var cm = this.scope.getColumnModel();
		
		for(i=0;i<cm.getColumnCount();i++){
			if (cm.isHidden(i)) continue;
			if (cm.getDataIndex(i)=='') continue;
			
			var fld=this.scope.store.fields.filter('name', cm.getColumnHeader(i))[0];
			if (fld){
				//pitaj za type i ovisno o tome dodaj input funkciju na input field; staviti direktno u json objekt ovdje samo za test
			};
		   fields.push({
				id:this.scope.id+cm.getDataIndex(i),
				xtype:'textfield',
				name:cm.getDataIndex(i),
				fieldLabel:cm.getColumnHeader(i),
				allowblank:false,
				anchor:'95%'
			});
		}
		
		return fields;
	},
	
	getWin : function(){		
		
		win = new Ext.Window({
			title : 'Advanced grid filter',
			items : [this],
			css : 'x-plain',
			modal : true,
			resizable   : true,
			draggable   : true,        		 
			layout : 'fit',
			autoHeight :  true
		});

		return win;
	}
});